
//var path ="localhost:8080";
	var ip ;
	getWsIp();
    function getWsIp(){
    	$.post("/fangyuan/getWebSocketIp",
    			function(data){
    				ip=data.result;
    				webSocket(ip);
    			}, "json");   	
    }

    var ws;
    function webSocket(path){
    	
    	if ('WebSocket' in window) {
    		console.log("1");
    		ws = new WebSocket("ws://" + path + "/Ds");
    	} else  {
    		console.log("2");
    		ws = new MozWebSocket("ws://" + path + "/Ds");
    	}
    	// 初始化一个 WebSocket 对象
    	//var ws = new WebSocket("ws://localhost:8080/ws");
    	
    	// 建立 web socket 连接成功触发事件
    	ws.onopen = function() {
    		// 使用 send() 方法发送数据
    		ws.send("发送数据");
    		console.log("数据发送中...");
    		heartCheck.start();
    	};
    	
    	// 接收服务端数据时触发事件
    	ws.onmessage = function(evt) {
    		heartCheck.reset();
    		//var received_msg = evt.data;
    		//console.log(evt.data);
    		zhongzhuan(evt.data)
    	};
    	
    	// 断开 web socket 连接成功触发事件
    	ws.onclose = function() {
    		console.log("连接已关闭...");
    		heartCheck.start();
    	};
    	  	
    	var heartCheck = {
  			  timeout: 5000,
  			  timeoutObj: null,
  			  reset: function(){
  				 //console.log("重置心跳包");
  			      clearTimeout(this.timeoutObj);
  			      this.start();
  			  },
  			  start: function(){
  				  //console.log("启动心跳包");
  			      this.timeoutObj = setTimeout(function(){webSocket(ip);console.log("心跳包发送");}, this.timeout)
  			  },
  			};
    }
    
	function zhongzhuan(msg){
		 var json = JSON.parse(msg);
		 var code = json.code;
		 if(code==="000"){
			 //console.log(json.result);
		 }else if(code==="001"){
			 //console.log(json.result);
			 //updateRoomState(json.result);
			 if(typeof updateRoomState != 'undefined' && updateRoomState instanceof Function){				 
				 updateRoomState(json.result);				 
			 }
			 if(typeof updateRoomCollectionCount != 'undefined' && updateRoomCollectionCount instanceof Function){
				 updateRoomCollectionCount(json.result);				 
			 }
		 }else if(code==="002"){
			 houseStateControl(json.result);
		 }else if(code==="003"){
			 if(typeof userGetOut != 'undefined' && userGetOut instanceof Function){
				 userGetOut(json.result);				 
			 }
		 }else if(code==="004"){
			 if(typeof countTime != 'undefined' && countTime instanceof Function){
				 countTime(json.result);//推送服务器时间
			 }
			 if(typeof daojs != 'undefined' && daojs instanceof Function){
				 daojs(json.result);
			 }
		 }else if(code==="005"){
				 console.log(json.result);//配置信息	
				 startTimeIni=json.result.StartTime.replace(/\-/g, "/");
				 ShoppingTimeIni=parseInt(json.result.ShoppingTime)*60;
				 maxtimeIni=parseInt(json.result.CountDownTime)*60;
				 alreadyBuyCount=0;
		 }
	}
	
		
